package com.old.controller;

import com.old.service.impl.BigModelService;
import com.old.util.BigModelNewUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RequiredArgsConstructor
public class BigModelController {

    private final BigModelService bigModelService;

    @GetMapping("/chat")
    public SseEmitter streamChat(@RequestParam String message ,String userId) {
//        SseEmitter emitter = new SseEmitter(30000L); // 30秒超时
        SseEmitter emitter = new SseEmitter(60000L); // 1分钟超时
        new Thread(() -> {
            try {
//                使用用户输入的消息启动聊天模型
                bigModelService.start(message, emitter,userId);
//                BigModelNewUtil.start(message,emitter);

            } catch (Exception e) {
                try {
                    emitter.send(SseEmitter.event().data("错误: " + e.getMessage()));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }
}

